//
//  ZGLsettings.m
//  Faker
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLsettings.h"
@interface ZGLsettings()
@property(nonatomic, retain)NSString *setPath;
@property(nonatomic, retain)NSMutableDictionary *setDic;
@end
@implementation ZGLsettings
//初始化
- (instancetype)init{
    self = [super init];
    if (self) {
//        self.setPath = [[NSBundle mainBundle]pathForResource:@"ZGLsettings" ofType:@"plist"];
//        self.setDic = [[NSDictionary alloc]initWithContentsOfFile:self.setPath].mutableCopy;
        
        //获取document路径
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        self.setPath = [documentsDirectory stringByAppendingPathComponent:@"ZGLsettings.plist"];
        
        //判断文件是否存在
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if(![fileManager fileExistsAtPath:self.setPath])//如果不存在
        {
            //获取程序包中相应文件的路径
            NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"ZGLsettings" ofType:@"plist"];
            //拷贝
            [fileManager copyItemAtPath:dataPath toPath:self.setPath error:nil];
        }
        
        self.setDic = [[NSDictionary alloc]initWithContentsOfFile:self.setPath].mutableCopy;
        
        NSString *str = [self.setDic objectForKey:@"Is_night"];
        if ([str isEqualToString:@"YES"]) {
            self.isNight = YES;
        }else{
            self.isNight = NO;
        }

        NSNumber *cs = [self.setDic objectForKey:@"ColorStyle"];
        self.navicolor = colorStyle[cs.integerValue];
        
        self.userName = nil;
    }
    return self;
}
//创建单例
+ (instancetype)shareSettings{
    static ZGLsettings *setting = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        setting = [[ZGLsettings alloc]init];
    });
    return setting;
}

- (void)changeNight{
    if (_isNight) {
        [self.setDic setValue:@"YES" forKey:@"Is_night"];
    }else{
        [self.setDic setValue:@"NO" forKey:@"Is_night"];
    }
    [self.setDic writeToFile:self.setPath atomically:YES];
}

- (void)changeNaviColor:(NSNumber *)style{
    [self.setDic setValue:style forKey:@"ColorStyle"];
    [self.setDic writeToFile:self.setPath atomically:YES];
}

//夜间模式
- (UIColor *)color:(nightColor)night{
    switch (night) {
        case FontColor:
            if (self.isNight) {
                return [UIColor whiteColor];
            }else{
                return [UIColor blackColor];
            }
            break;
        case TintColor:
            if (self.isNight) {
                return [UIColor colorWithRed:75/255.0 green:75/255.0 blue:75/255.0 alpha:1];
            }else{
                return [UIColor whiteColor];
            }
            break;
        case BgColor:
            if (self.isNight) {
                return [UIColor colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:1];;
            }else{
                return [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1];
            }
            break;
        default:
            return [UIColor blackColor];
            break;
    }
}

- (void)log:(NSString *)str{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:str message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
}
@end
